<?php
class Default_Model_DbTable_Object extends Zend_Db_Table_Abstract {
	protected $_name = 'object';
	public function addObject($page_id, $object_img, $object_name, $title, $short_description, $long_description, $detail, $email, $classify, $status) {
		$data = array (
				'PAGE_ID' => $page_id,
				'OBJECT_IMG' => $object_img,
				'OBJECT_NAME' => $object_name,
				'TITLE' => $title,
				'SHORT_DESCRIPTION' => $short_description,
				'LONG_DESCRIPTION' => $long_description,
				"DETAIL"=> $detail,
				"EMAIL" => $email,
				"CLASSIFY" => $classify,
				'STATUS' => $status 
		);
		return $this->insert ( $data );
	}
	public function updateObject($id, $page_id, $object_img, $object_name, $title, $short_description, $long_description, $detail, $email, $classify,$status) {
		$data = array (
				'PAGE_ID' => $page_id,
				'OBJECT_IMG' => $object_img,
				'OBJECT_NAME' => $object_name,
				'TITLE' => $title,
				'SHORT_DESCRIPTION' => $short_description,
				'LONG_DESCRIPTION' => $long_description,
				"DETAIL"=> $detail,
				"EMAIL" => $email,
				"CLASSIFY" => $classify,
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID = $id" );
	}
	public function updateStatus($id, $status) {
		$data = array (
				'STATUS' => $status 
		);
		return $this->update ( $data, "ID=$id" );
	}
	public function removeObject($id) {
		return $this->delete ( "ID = $id" );
	}
	public function activeObject($id) {
		$lv = $this->fetchRow ( "ID = $id" );
		if ($lv ["STATUS"] == 1) {
			$this->update ( array (
					"STATUS" => 0 
			), "ID = $id" );
		} else {
			$this->update ( array (
					"STATUS" => 1 
			), "ID = $id" );
		}
	}
	public function getObjectByPageId($page_id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		try {
			$rows = $this->fetchAll ("PAGE_ID = $page_id AND STATUS = 1");
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getObjectByPageName($page_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object AS o", array (
				"o.ID",
				"o.OBJECT_IMG",
				"o.OBJECT_NAME",
				"o.TITLE",
				"o.SHORT_DESCRIPTION",
				"o.LONG_DESCRIPTION",
				"o.STATUS" 
		) )->where ( "o.STATUS = ?", 1 )->join ( "page AS p", "p.ID = o.PAGE_ID", array (
				"p.PAGE_NAME" 
		) )->where ( "p.PAGE_NAME= ?", $page_name );
		try {
			$rows = $this->fetchAll ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function getAllObject() {
		return $this->fetchAll ( "STATUS = 1", "ID DESC" )->toArray ();
	}
	public function getObjectById($id) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object AS o", array (
				"o.ID",
				"o.PAGE_ID",
				"o.OBJECT_IMG",
				"o.OBJECT_NAME",
				"o.TITLE",
				"o.SHORT_DESCRIPTION",
				"o.LONG_DESCRIPTION",
				"o.EMAIL",
				"o.DETAIL",
				"o.STATUS" 
		) )->where ( "o.STATUS = ?", 1 )->where ( "o.ID= ?", $id );
		try {
			$row = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}

		return $row;
	}
	
	public function getById($id) {
		$rows = NULL;
		
		try {
			$row = $this->fetchRow ( "ID = $id" );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
	
		return $row;
	}
	
	public function getObjectByName($object_name) {
		$select = $this->select ( Zend_Db_Table::SELECT_WITH_FROM_PART )->setIntegrityCheck ( false );
		
		$rows = NULL;
		
		$select->from ( "object AS o", array (
				"o.ID",
				"o.OBJECT_IMG",
				"o.OBJECT_NAME",
				"o.TITLE",
				"o.SHORT_DESCRIPTION",
				"o.LONG_DESCRIPTION",
				"o.STATUS" 
		) )->where ( "o.STATUS = ?", 1 )->join ( "page AS p", "p.ID = o.PAGE_ID", array (
				"p.PAGE_NAME" 
		) )->where ( "o.OBJECT_NAME= ?", $object_name );
		try {
			$rows = $this->fetchRow ( $select );
		} catch ( Exception $e ) {
			print_r ( $e );
		}
		return $rows->toArray ();
	}
	public function ShowAllProduct($count, $offset, $detail, $page_id){
		$select = $this->select ()->from ( $this->_name, array (
				"ID",
				"OBJECT_NAME",
				"CLASSIFY",
				"OBJECT_IMG"
		) )->where ( "DETAIL = '$detail' AND PAGE_ID = $page_id" )->order ( "ID DESC" )->limit ( $count, $offset );
		return $this->fetchAll ( $select )->toArray ();
	}
	
	public function ShowAllArchitect($count, $offset, $detail) {
		$select = $this->select ()->from ( $this->_name, array (
				"ID",
				"OBJECT_NAME",
				"OBJECT_IMG" 
		) )->where ( "DETAIL = '$detail'" )->order ( "ID DESC" )->limit ( $count, $offset );
		return $this->fetchAll ( $select )->toArray ();
	}
	public function getRowCount($detail) {
		$select = $this->select ()->from ( $this->_name, array (
				"ID",
				"OBJECT_NAME",
				"OBJECT_IMG" 
		) )->where ( "DETAIL = '$detail'" );
		return sizeof ( $this->fetchAll ( $select )->toArray () );
	}
	public function ShowArcByID($id) {
		$select = $this->select ()->from ( $this->_name, "ID" );
	}
	
	public function ShowObjByDetai($detail){
		$select = $this->select()
		->from($this->_name)
		->where("DETAIl = '$detail'")
		->order("ID DESC");
		return $this->fetchAll($select)->toArray();
	}
}

